package myapps.myservlet;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OnlineUsers
 */
public class OnlineUsers extends HttpServlet 
{
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public OnlineUsers() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException 
	{
		
		PrintWriter out = res.getWriter();
      
		try 
		{
			Class.forName("com.mysql.jdbc.Driver");
			
			Connection connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/chat_group_db", "root", "");
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery("SELECT  Username FROM user_online");
			
			out.println("<b> Online Users: </b><br/>");
			
			while (resultSet.next()) 
			{
				out.println(resultSet.getString(1) + "<br/>");

			}
		} 
		catch (Exception e) 
		{
			
			e.printStackTrace();
		}

	}// end post

}//end class
